﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.DataModel;
using Giaiphongmatbang.BLL;

namespace Giaiphongmatbang
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        User user;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                user = (User)Session["User"];
                Bindgrv_ListLand();
                if (user == null)
                {
                    ibtnAdd.Visible = false;
                }
            }
        }

        protected void Bindgrv_ListLand()
        {
            DataAccess.GPMBDataContext datacontext = new DataAccess.GPMBDataContext();
            grv_ListLand.AutoGenerateColumns = false;
            grv_ListLand.DataSource = datacontext.uspGetAllLandType();
            grv_ListLand.DataBind();
        }

        protected void btnAddLand_Click(object sender, EventArgs e)
        {
            DataAccess.GPMBDataContext datacontext = new DataAccess.GPMBDataContext();
            Boolean modenew = Boolean.Parse(hdfmodenew.Value);
            if (modenew)
            {
                float heso = 1;
                bool issupport = false;
                if (chk_Support.Checked == true)
                {
                    issupport = true;
                }
                datacontext.uspAddNewLand(txtLandName.Text, heso, issupport, txtDescriptions.Text);
            }
            else
            {
                int idland = int.Parse(hdf.Value);
                float heso = 1;
                bool issupport = false;
                if (chk_Support.Checked == true)
                {
                    issupport = true;
                }
                datacontext.uspEditLand(idland, txtLandName.Text, heso, issupport, txtDescriptions.Text);
            }
            Bindgrv_ListLand();
        }

        protected void grv_ListLand_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandname = e.CommandName;
            try
            {
                int landid = int.Parse(e.CommandArgument.ToString());
                if (commandname.Equals("ibtnDelete"))
                {
                    LandRepository objLand = new LandRepository();
                    objLand.DeleteByID(landid);
                    Bindgrv_ListLand();
                }
            }
            catch { }
        }

        protected void grv_ListLand_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (Session["User"] == null)
            {
                int columnCount = grv_ListLand.Columns.Count;
                grv_ListLand.Columns[columnCount - 1].Visible = false;
                return;
            }
        }
    }
}